package com.yunfei.commonbase.hdutil.conreoller;

import com.alibaba.fastjson.JSONObject;
import com.yunfei.commonbase.hdutil.FTPUtil;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiOperation;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import javax.servlet.http.HttpServletResponse;
import java.io.InputStream;
import java.io.OutputStream;
import java.util.ArrayList;
import java.util.List;

@RestController
@RequestMapping(value = "/ftp")
public class FtpController {
    @ApiOperation(value = "ftp文件上传",notes = "<pre>\n" +
            "入参报文如下\n" +
            "{\n" +
            "    \"ip\": \"\",\n" +
            "    \"port\": \"\",\n" +
            "    \"username\": \"\",\n" +
            "    \"password\": \"\",\n" +
            "    \"ftpPath\": \"\"\n" +
            "}\n" +
            "参数说明：\n" +
            "ip：       ftpIP地址\n" +
            "port：     ftp端口\n" +
            "username： ftp用户名\n" +
            "password： ftp密码\n" +
            "ftpPath：  要上传的路径\n" +
            "</pre>")
    @PostMapping(value = "/upload")
    public void upload(String requestJson, MultipartFile file) {
        try {
            JSONObject obj = JSONObject.parseObject(requestJson);
            String ip = obj.getString("ip");
            int port = obj.getInteger("port");
            String username = obj.getString("username");
            String password = obj.getString("password");
            String ftpPath = obj.getString("ftpPath");
            String filename = file.getOriginalFilename();
            InputStream is = file.getInputStream();
            FTPUtil.upload(ip, port, username, password, ftpPath, filename, is);
        } catch (Exception e) {
            e.printStackTrace();
        }
    }

    @ApiOperation(value = "ftp文件下载",notes = "<pre>\n" +
            "入参报文如下\n" +
            "{\n" +
            "    \"ip\": \"\",\n" +
            "    \"port\": \"\",\n" +
            "    \"username\": \"\",\n" +
            "    \"password\": \"\",\n" +
            "    \"ftpPath\": \"\",\n" +
            "\t\"filename\":\"\"\n" +
            "}\n" +
            "参数说明：\n" +
            "ip：       ftpIP地址\n" +
            "port：     ftp端口\n" +
            "username： ftp用户名\n" +
            "password： ftp密码\n" +
            "ftpPath：  要下载的路径\n" +
            "filename:  要下载的文件名称\n" +
            "</pre>")
    @RequestMapping(value = "/download",method = RequestMethod.POST)
    public void download(@RequestBody String requestJson, HttpServletResponse response) {
        try {
            JSONObject obj = JSONObject.parseObject(requestJson);
            String ip = obj.getString("ip");
            int port = obj.getInteger("port");
            String username = obj.getString("username");
            String password = obj.getString("password");
            String ftpPath = obj.getString("ftpPath");
            String filename = obj.getString("filename");
            response.setContentType("application/octet-stream");
            response.setHeader("content-type", "application/octet-stream");
            response.setHeader("Content-Disposition", "attachment;fileName=" + java.net.URLEncoder.encode(filename, "UTF-8"));// 设置文件名
            OutputStream os = response.getOutputStream();
            FTPUtil.downLoad(ip, port, username, password, os, ftpPath, filename);
        } catch (Exception e) {
            e.printStackTrace();
        }
    }


    @ApiOperation(value = "ftp文件删除",notes = "<pre>\n" +
            "入参报文如下\n" +
            "{\n" +
            "    \"ip\": \"\",\n" +
            "    \"port\": \"\",\n" +
            "    \"username\": \"\",\n" +
            "    \"password\": \"\",\n" +
            "    \"directory\": \"\"\n" +
            "}\n" +
            "参数说明：\n" +
            "ip：       ftpIP地址\n" +
            "port：     ftp端口\n" +
            "username： ftp用户名\n" +
            "password： ftp密码\n" +
            "directory：要删除的目录\n" +
            "</pre>")
    @PostMapping(value = "/delete")
    @ApiImplicitParam(name = "requestJson", value = "requestJson", required = true, dataType = "string", paramType = "body")
    public String delete(@RequestBody String requestJson) {
        try {
            JSONObject obj = JSONObject.parseObject(requestJson);
            String ip = obj.getString("ip");
            int port = obj.getInteger("port");
            String username = obj.getString("username");
            String password = obj.getString("password");
            String directory = obj.getString("directory");
            FTPUtil.delete(ip, port, username, password, directory);
        } catch (Exception e) {
            return e.getMessage();
        }
        return "SUCCESS";
    }

    @ApiOperation(value = "ftp目录下文件查询",notes = "<pre>\n" +
            "入参报文如下\n" +
            "{\n" +
            "    \"ip\": \"\",\n" +
            "    \"port\": \"\",\n" +
            "    \"username\": \"\",\n" +
            "    \"password\": \"\",\n" +
            "    \"directory\": \"\"\n" +
            "}\n" +
            "参数说明：\n" +
            "ip：       ftpIP地址\n" +
            "port：     ftp端口\n" +
            "username： ftp用户名\n" +
            "password： ftp密码\n" +
            "directory：要查询的目录\n" +
            "</pre>")
    @PostMapping(value = "/getFileList")
    @ApiImplicitParam(name = "requestJson", value = "requestJson", required = true, dataType = "string", paramType = "body")
    public Object getFileList(@RequestBody String requestJson) {
        List<String> fileList = new ArrayList<>();
        try {
            JSONObject obj = JSONObject.parseObject(requestJson);
            String ip = obj.getString("ip");
            int port = obj.getInteger("port");
            String username = obj.getString("username");
            String password = obj.getString("password");
            String directory = obj.getString("directory");
            fileList = FTPUtil.getFileList(ip, port, username, password, directory);
        } catch (Exception e) {
            return e.getMessage();
        }
        return fileList;
    }
}
